--[[=========================================================
	WEAPON
=========================================================]]--

--[[-----------------------------------------------------]]--
--	Constructor
--[[-----------------------------------------------------]]--
ObjWeapon						= class( function(
								self,
								id,
								base,
								name,
								display,
								autoSwitchTo,
								autoSwitchFrom,
								csMuzzleFlashes,
								spawnable,
								adminSpawnable )

	--	Members
	self.TableName				= "Weapon"
	self.ID						= ValOrDef( id, "integer", 0 )
	self.Base					= ValOrDef( base, "string", "weapon_sigma_test" )
	self.Name					= ValOrDef( name, "string", "" )
	self.Display				= ValOrDef( display, "string", "" )
	self.AutoSwitchTo			= ValOrDef( autoSwitchTo, "boolean", false )
	self.AutoSwitchFrom			= ValOrDef( autoSwitchFrom, "boolean", false )
	self.CSMuzzleFlashes		= ValOrDef( csMuzzleFlashes, "boolean", false )
	self.Spawnable				= ValOrDef( spawnable, "boolean", false )
	self.AdminSpawnable			= ValOrDef( adminSpawnable, "boolean", false )
	
--	SQL management - SERVER only
if SERVER then
	
	--	SQL Insert ( table, ids, contents )
	self.Insert					= function( self )
	
		--	Variables
		local result			= nil
	
		--	Generate a new ID
		if self.ID == 0 then
			self.ID				=
				sql.GetNewID(
					self.TableName,
					FieldInfo( "ID", self.ID, "number" )
				)
		end
		
		--	Insert in DB if we got an ID
		if self.ID != 0 then
			result = sql.Insert(
				self.TableName,
				{ 1 },
				FieldInfo( "ID", self.ID, "number" ),
				FieldInfo( "Base", self.Base, "string" ),
				FieldInfo( "Name", self.Name, "string" ),
				FieldInfo( "Display", self.Display, "string" ),
				FieldInfo( "AutoSwitchTo", self.AutoSwitchTo, "boolean" ),
				FieldInfo( "AutoSwitchFrom", self.AutoSwitchFrom, "boolean" ),
				FieldInfo( "CSMuzzleFlashes", self.CSMuzzleFlashes, "boolean" ),
				FieldInfo( "Spawnable", self.Spawnable, "boolean" ),
				FieldInfo( "AdminSpawnable", self.AdminSpawnable, "boolean" )
			)
		end
	end
	
	--	SQL Update ( table, ids, contents )
	self.Update					= function( self )
	
		--	Variables
		local result			= nil
	
		--	Update in DB
		result = sql.Update(
			self.TableName,
			{ 1 },
			FieldInfo( "ID", self.ID, "number" ),
			FieldInfo( "Base", self.Base, "string" ),
			FieldInfo( "Name", self.Name, "string" ),
			FieldInfo( "Display", self.Display, "string" ),
			FieldInfo( "AutoSwitchTo", self.AutoSwitchTo, "boolean" ),
			FieldInfo( "AutoSwitchFrom", self.AutoSwitchFrom, "boolean" ),
			FieldInfo( "CSMuzzleFlashes", self.CSMuzzleFlashes, "boolean" ),
			FieldInfo( "Spawnable", self.Spawnable, "boolean" ),
			FieldInfo( "AdminSpawnable", self.AdminSpawnable, "boolean" )
		)
	end
	
	--	SQL Delete ( table, ids, id contents )
	self.Delete					= function( self )
	
		--	Variables
		local result			= nil
	
		--	Delete from DB
		result = sql.Delete(
			self.TableName,
			{ 1 },
			FieldInfo( "ID", self.ID, "number" )
		)
		
		--	Reset ID if it was deleted
		if result then
			self.ID				= 0
		end
	end
	
	--	SQL Reload ( table, ids, id contents )
	self.Reload					= function( self, id )
	
		--	Redefinie id if needed
		if IsInt( id ) then
			self.ID				= id
		end
	
		--	Retrieve from DB
		self:FillProperties(
			sql.GetRow(
				self.TableName,
				{ 1 },
				FieldInfo( "ID", self.ID, "number" ),
				FieldInfo( "Base", self.Base, "string" ),
				FieldInfo( "Name", self.Name, "string" ),
				FieldInfo( "Display", self.Display, "string" ),
				FieldInfo( "AutoSwitchTo", self.AutoSwitchTo, "boolean" ),
				FieldInfo( "AutoSwitchFrom", self.AutoSwitchFrom, "boolean" ),
				FieldInfo( "CSMuzzleFlashes", self.CSMuzzleFlashes, "boolean" ),
				FieldInfo( "Spawnable", self.Spawnable, "boolean" ),
				FieldInfo( "AdminSpawnable", self.AdminSpawnable, "boolean" )
			)
		)
	end
	
	--	Assign fields from row
	self.FillProperties			= function( self, row )
		self.Base				= tostring( row["Base"] )
		self.Name				= tostring( row["Name"] )
		self.Display			= tostring( row["Display"] )
		self.AutoSwitchTo		= tobool( row["AutoSwitchTo"] )
		self.AutoSwitchFrom		= tobool( row["AutoSwitchFrom"] )
		self.CSMuzzleFlashes	= tobool( row["CSMuzzleFlashes"] )
		self.Spawnable			= tobool( row["Spawnable"] )
		self.AdminSpawnable		= tobool( row["AdminSpawnable"] )
	end
	
end	--	End of SQL management

--	Other server functions
if SERVER then

	--	Returns a table for transfers to clients
	self.GetClientTable				= function( self )
		return {
			["ID"]					= self.ID,
			["Base"]				= self.Base,
			["Name"]				= self.Name,
			["Display"]				= self.Display,
			["AutoSwitchTo"]		= self.AutoSwitchTo,
			["AutoSwitchFrom"]		= self.AutoSwitchFrom,
			["CSMuzzleFlashes"]		= self.CSMuzzleFlashes,
			["Spawnable"]			= self.Spawnable,
			["AdminSpawnable"]		= self.AdminSpawnable
		}
	end

end
	
end)